import io.reactivex.ObservableEmitter;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;
import top.lingkang.potato.network.HttpRequest;
import top.lingkang.potato.utils.rx.ByWatch;
import top.lingkang.potato.utils.rx.RxObservableUtils;
import top.lingkang.potato.utils.rx.Watch;

/**
 * @author lingkang
 * @date 2021/9/17 22:04
 * @description
 */
public class JavaFxDemo extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        HttpRequest.initHttpRequest();
        // 定义一个标签，类似html中的span
        Label label = new Label("hello world!");

        // 将标签加入场景，场景类似 HTML中的 body， 将span放到body中
        // 将场景放到stage中，类似HTML 中的将 body 放到 html标签里一样
        stage.setScene(new Scene(label));

        // 设置舞台的宽高标题
        stage.setWidth(400);
        stage.setHeight(100);
        stage.setTitle("师姐，你好！");
        stage.show();

        new RxObservableUtils(new ByWatch() {
            @Override
            public void subscribe(ObservableEmitter emitter) throws Exception {
                while (true) {
                    // http 同步请求获取服务上线进度
                    int info = HttpRequest.userClient.getUpInfo();
                    emitter.onNext(info);
                    if (info >= 100) {
                        emitter.onComplete();
                        break;
                    }
                }
            }
        }).watch(new Watch() {
            @Override
            public void onNext(Object o) {
                label.setText("上线服务完成：" + o + " %");
            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
                label.setText("获取进度异常");
            }
        });

        /*// 被观察者对象
        Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
                while (true){
                    int info= HttpRequest.userClient.getUpInfo();
                    emitter.onNext(info);
                    if (info>=100){
                        emitter.onComplete();
                        break;
                    }
                }
            }
        }).subscribeOn(Schedulers.io()) //在子线程发射
                .observeOn(Schedulers.from(new Executor() {
                    @Override
                    public void execute(Runnable command) {
                        // 将线程执行发往主线程更新UI
                        Platform.runLater(command);
                    }
                }))  //在主线程接收执行更新UI
                .subscribe(new Observer<Integer>() {// 观察对象
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Integer integer) {
                        label.setText("上线服务完成："+integer+" %");
                    }

                    @Override
                    public void onError(Throwable e) {
                        label.setText("获取进度异常");
                    }

                    @Override
                    public void onComplete() {

                    }
                });

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        // http 同步请求获取服务上线进度
                        int info= HttpRequest.userClient.getUpInfo();
                        Platform.runLater(new Runnable() {
                            @Override
                            public void run() {
                                label.setText("上线服务完成："+info+" %");
                            }
                        });
                        if (info>=100){
                            break;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        label.setText("获取进度异常");
                        break;
                    }
                }
            }
        }).start();*/
    }

    public static void main(String[] args) {
        launch(args);
    }
}
